package map;
//散列表  键值对图 无序

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapDemo {
    /**
     * 通过entrySet遍历 相当于每次把一个键值对映射出来 就是Map.entry然后获取
     * @param hashMap
     */
    public static void entrySet(HashMap<Integer,String> hashMap){
        Iterator<Map.Entry<Integer,String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer,String> entry = iterator.next();
            System.out.println("键："+entry.getKey()+"值："+entry.getValue());
        }
    }

    /**
     * 通过键来遍历
     * @param hashMap
     */
    public static void keySet(HashMap<Integer,String > hashMap){
        Iterator<Integer> integerIterator = hashMap.keySet().iterator();
        while(integerIterator.hasNext()){
            Integer key = integerIterator.next();
            System.out.println("键："+key+"值："+hashMap.get(key));
        }
    }

    /**
     * 通过foreach循环遍历  跟entrySet遍历基本一致
     * @param hashMap
     */
    public static void foreachEntry(HashMap<Integer,String > hashMap){
        for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
            System.out.println("键："+entry.getKey()+"值："+entry.getValue());
        }
    }

    /**
     * 同上，换成key
     * @param hashMap
     */
    public static void foreachKey(HashMap<Integer,String > hashMap){
        for (Integer integer : hashMap.keySet()) {
            System.out.println("键："+integer+"值："+hashMap.get(integer));
        }
    }

    /**
     * lambda方法
     * @param hashMap
     */
    public static void lambda(HashMap<Integer,String > hashMap){
        hashMap.forEach((key,value)->{
            System.out.println("键："+key+"值："+value);
        });
    }
    public static void main(String[] args) {
        HashMap<Integer,String> hashMap = new HashMap<Integer, String>();
        hashMap.put(1,"yumingmin");
        hashMap.put(2,"wanglubao");
        hashMap.put(3,"kouminghao");
        hashMap.put(4,"luoxuanfeng");
//        hashMap.remove(3);
        System.out.println("迭代");
        Iterator<Map.Entry<Integer,String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer,String> entry = iterator.next();
            System.out.println(entry.getKey()+entry.getValue());
        }
        System.out.println("Entryset方法");
        entrySet(hashMap);
        System.out.println("keyset方法");
        keySet(hashMap);
        System.out.println("foreachEntry方法");
        foreachEntry(hashMap);
        System.out.println("foreachKey(hashMap)");
        foreachKey(hashMap);
        System.out.println("lambda");
        lambda(hashMap);

    }



}
